package com.mirabelle.juice.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mirabelle.juice.beans.Paginator;
import com.mirabelle.juice.model.Collection;
import com.mirabelle.juice.service.CollectionService;

@Controller("C_COLLECTION")
@Scope("session")
public class CCollection implements Serializable {
	private static final long serialVersionUID = 1L;
	private transient Logger log = LoggerFactory.getLogger(getClass());

	private String name = null;
	private String comment = null;
	private List<Collection> collections = new ArrayList<Collection>();
	private Collection currentCollection = null;
	private Paginator<Collection>   paginator		= null;

	private boolean displayDetail = false;
	private boolean newCollection = false;

	@Autowired
	@Qualifier("CollectionService")
	protected CollectionService collectionService;
	@Autowired
	@Qualifier("C_LOGGED_AUTHOR")
	private CPreferences authorPreferences;

	@PostConstruct
	public void init() {
		log.debug("Initialisation du controlleur C_COLLECTION.");
		paginator = new Paginator<Collection>(collections, 3);
		if (null != authorPreferences.getLoggedAuthor()) {
			log.debug("L'utilisateur connecté est : "
					+ authorPreferences.getLoggedAuthor().getMail());
			collections = authorPreferences.getLoggedAuthor().getCollections();
			log.debug("Collections : " + collections + " elements : "
					+ collections.size());
		}
		paginator.setList(collections);
	}

	public String save() {
		if (newCollection) {
			log.debug("Création de la collection de nom : " + name
					+ " et de commentaire : " + comment);
			currentCollection = new Collection();
			currentCollection.setAuthor(authorPreferences.getLoggedAuthor());
			currentCollection.setCreated(new Date());
		} else {
			log.debug("Modification de la collection courante de nom : "
					+ currentCollection.getName() + " et de commentaire : "
					+ currentCollection.getComment());
		}
		currentCollection.setModified(new Date());
		currentCollection.setName(name);
		currentCollection.setComment(comment);
		collectionService.saveCollection(currentCollection);
		if (newCollection) {
			authorPreferences.getLoggedAuthor().getCollections()
					.add(currentCollection);
			newCollection = false;
		}
		paginator.setList(collections);
		paginator.getLast();
		displayDetail = false;
		return "collection";
	}

	public String remove() {
		log.debug("Suppression de la collection de nom : " + currentCollection.getName()
				+ " et de commentaire : " + currentCollection.getComment());
		collectionService.removeCollection(currentCollection);
		authorPreferences.getLoggedAuthor().getCollections()
				.remove(currentCollection);
		paginator.setList(collections);
		displayDetail = false;
		return "collection";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Collection getCurrentCollection() {
		return currentCollection;
	}

	public void setCurrentCollection(Collection currentCollection) {
		log.debug("La collection courante est maintenant de nom  : "
				+ currentCollection.getName() + " et de commentaire : "
				+ currentCollection.getComment());
		this.currentCollection = currentCollection;
	}

	public List<Collection> getCollections() {
		return collections;
	}

	public void setCollections(List<Collection> collections) {
		this.collections = collections;
	}

	public boolean isDisplayDetail() {
		return displayDetail;
	}

	public void setDisplayDetail(boolean displayDetail) {
		this.displayDetail = displayDetail;
	}

	public boolean isNewCollection() {
		return newCollection;
	}

	public void setNewCollection(boolean newCollection) {
		this.newCollection = newCollection;
	}
	
	public Paginator<Collection> getPaginator() {
		/*
		if (paginator.getPsize() == 0) {
			if (null != authorPreferences.getLoggedAuthor()) {
				log.debug("L'utilisateur connecté est : "
						+ authorPreferences.getLoggedAuthor().getMail());
				collections = authorPreferences.getLoggedAuthor().getCollections();
				log.debug("Collections : " + collections + " elements : "
						+ collections.size());
			}
			paginator.setList(collections);
		}
		*/
		return paginator;
	}
	
	public boolean isNext() {
		return paginator.hasNext();
	}
	
	public boolean isPrevious() {
		return paginator.hasPrevious();
	}
	
	public void getNext() {
		paginator.getNext();
	}
	
	public void getPrevious() {
		paginator.getPrevious();
	}
}
